using System;
using System.Text;
using System.Web;
using System.Web.UI;

namespace Q.Framework.Utility
{
    static public class JavaScriptAdd
    {
        static public String HTMLEncode(String s)
        {

            s = s.Replace(Environment.NewLine, "\n");
            //s = s.Replace(#13, '\n');
            //s = s.Replace(#10, '\n');
            return s;
        }

        static public void RegisterStartupScript(Page AOwner, String Key, StringBuilder Code)
        {
            if (!AOwner.ClientScript.IsStartupScriptRegistered(Key))
            {
                //ScriptManager.RegisterClientScriptBlock(AOwner, AOwner.GetType(), Key, Code.ToString(), true);
                AOwner.ClientScript.RegisterStartupScript(AOwner.GetType(), Key, Code.ToString());
                //AOwner.RegisterStartupScript(Key, Code.ToString());
                //ScriptManager.RegisterStartupScript(AOwner, AOwner.GetType(), Key, Code.ToString(), false);
            }
        }

        static public void RegisterStartupScriptAjax(Control control, Page AOwner, String Key, StringBuilder Code)
        {
            if (!AOwner.ClientScript.IsStartupScriptRegistered(Key))
            {
                AOwner.ClientScript.RegisterClientScriptBlock(AOwner.GetType(), "Focus", Code.ToString(), true);
                AOwner.ClientScript.RegisterStartupScript(AOwner.GetType(), Key, Code.ToString());
                // AOwner.RegisterStartupScript(Key, Code.ToString());
            }
        }

        static public void PrintStartScript(StringBuilder Code)
        {
            Code.Append("" + Environment.NewLine);
        }

        static public void PrintConfirm(String Question, StringBuilder Code)
        {
            Code.Append("if (confirm('" + HTMLEncode(Question) + "')) ");
        }

        static public void PrintRedirect(String ToPage, StringBuilder Code)
        {

            Code.Append("  window.location.href = '" +
              HttpContext.Current.Response.ApplyAppPathModifier(ToPage) + "'");

        }

        static public void PrintAlert(String Information, StringBuilder Code)
        {
            Code.Append("alert('" + HTMLEncode(Information.Replace("\r\n", "\\n")) + "');");
        }

        static public void SetFocus(Page AOwner, String ctrlID)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("document.getElementById('");
            sb.Append(ctrlID);
            sb.Append("').focus()");
            AddScriptTag(AOwner, "SetFocus", sb);
        }

        static private void AddScriptTag(Page AOwner, String key, StringBuilder sb)
        {
            StringBuilder sB = new StringBuilder();
            sB.Append("<script language='javascript'>");
            sB.Append(sb.ToString());
            sB.Append("</script>");

            RegisterStartupScript(AOwner, key, sB);
        }

        static public void MessageBoxShow(Page AOwner, String msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("window.alert('");
            sb.Append(msg.Replace(Environment.NewLine, "<br />"));
            sb.Append("')");
            AddScriptTag(AOwner, "MessageBox", sb);
        }

        static public void WindowOpen(Page AOwner, String url)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("window.open('" + url + "', '', 'menubar=no,toolbars=no,scrollbars=no,directories=no,fullscreen=yes,resizable=no,status=no,location=no');");
            AddScriptTag(AOwner, "OpenWindow", sb);
        }

        static public void WindowReportOpen(Page AOwner)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("window.open('../../../../ReportViewer.aspx', '', 'menubar=0,toolbars=0,scrollbars=0');");
            AddScriptTag(AOwner, "OpenWindow", sb);
        }

        //static public void MessageBoxShowRedrect(Page AOwner, String msg, String pageRedirect)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    sb.Append("window.alert('");
        //    sb.Append(msg);
        //    sb.Append("')");
        //    AddScriptTag(AOwner, "MessageBox", sb);
        //    PageRedirect(AOwner, pageRedirect);
        //}

        //static public void PageRedirect(Page AOwner, String pageRedrect)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    sb.Append("  window.location.href = '" + pageRedrect + "'");
        //    AddScriptTag(AOwner, "Redirect", sb);
        //}
    }
}